package jmind.core.image.code;

import jmind.core.image.ImageUtil;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;

/**
 *  * https://blog.csdn.net/qq_36892341/article/details/86644580
 * @author weibo
 * @date 2019/07/16
 */
public class ImageCodeUtil {

    static int targetLength=55;//小图长
    static int targetWidth=45;//小图宽
    static int circleR=7;//半径
    static int r1=4;//距离点



    /**
     *
     * @Createdate: 2019年1月24日上午10:52:42
     * @Title: getBlockData
     * @Description: 生成小图轮廓
     * @author mzl
     * @return int[][]
     * @throws
     */
    private static int[][] getBlockData() {

        int[][] data = new int[targetLength][targetWidth];
        double x2 = targetLength-circleR;

        //随机生成圆的位置
        double h1 = circleR + Math.random() * (targetWidth-3*circleR-r1);
        double po = circleR*circleR;

        double xbegin = targetLength-circleR-r1;
        double ybegin = targetWidth-circleR-r1;

        for (int i = 0; i < targetLength; i++) {
            for (int j = 0; j < targetWidth; j++) {
                double d3 = Math.pow(i - x2,2) + Math.pow(j - h1,2);
                double d2 = Math.pow(j-2,2) + Math.pow(i - h1,2);
                if ((j <= ybegin && d2 <= po)||(i >= xbegin && d3 >= po)) {
                    data[i][j] = 0;
                }  else {
                    data[i][j] = 1;
                }

            }
        }
        return data;
    }




    /**
     *
     * @Createdate: 2019年1月24日上午10:51:30
     * @Title: cutByTemplate
     * @Description: 生成小图片、给大图片添加阴影
     * @author mzl
     * @param oriImage
     * @param targetImage
     * @param templateImage
     * @param x
     * @param y void
     * @throws
     */
    private static void cutByTemplate(BufferedImage oriImage, BufferedImage targetImage, int[][] templateImage, int x, int y){
        for (int i = 0; i < targetLength; i++) {
            for (int j = 0; j < targetWidth; j++) {
                int rgb = templateImage[i][j];
                // 原图中对应位置变色处理
                int rgb_ori = oriImage.getRGB(x + i, y + j);

                if (rgb == 1) {
                    //抠图上复制对应颜色值
                    targetImage.setRGB(i, j, rgb_ori);
                    //原图对应位置颜色变化
                    oriImage.setRGB(x + i, y + j, rgb_ori & 0x363636 );
                }else{
                    //这里把背景设为透明
                    targetImage.setRGB(i, j, rgb_ori & 0x00ffffff);
                }
            }
        }
    }


    /**
     *
     * @Createdate: 2019年1月24日上午11:49:42
     * @Title: createImage
     * @Description: 获取大图，小图Base64码
     * @author mzl
     * @param url
     * @throws
     */
    public static ImageCode createImage(String url, int x, int y){
        ImageCode  imageCode=new ImageCode();
        imageCode.setX(x);
        imageCode.setY(y);
        try {
            BufferedImage bufferedImage = ImageIO.read(new FileInputStream(url));
            BufferedImage template= new BufferedImage(targetLength, targetWidth, BufferedImage.TYPE_4BYTE_ABGR);
            cutByTemplate(bufferedImage,template,getBlockData(),x,y);

          //  ImageIO.write(bufferedImage,"png",new File("/Users/weibo/Downloads/vcode/old3.jpg"));
         //   ImageIO.write(template,"png",new File("/Users/weibo/Downloads/vcode/min3.jpg"));



            //大图
            imageCode.setTargetImage(bufferedImage);
            //小图
            imageCode.setTemplateImage(template);
            //Map<String,String> resultMap=new HashMap<>();
            //resultMap.put("b", ImageUtil.image2Base64(bufferedImage));
             //resultMap.put("s", ImageUtil.image2Base64(template));

        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            return imageCode;
        }
    }

    public static void main(String[] args) {
      //  createImage("/Users/weibo/Downloads/vcode/2.jpg", 250, 430);
        for(int i=1;i<10;i++){
            BufferedImage image = (BufferedImage)ImageUtil.getImage(
                "/Users/weibo/workspace/learn/TOOLS_other/util/validate/src/main/resources/static/targets/" + i
                    + ".jpg");
            System.err.println(image.getWidth()+"----"+image.getHeight());

        }

    }

}
